﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Printing;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
using System.Xml;
using WPFExcelReport.Helpers;
using System.Printing;
namespace WPFExcelReport
{
    public class FontInfo
    {
        public FontInfo()
        {

        }
        public int CFontID;
        public string SymbolSet;
        public string FontSpacing;
        public double FontPitch = 16.67;
        public double FontUnit = 1 / 72;
        public string FontStyle;
        public string FontWeight;
        public string FontFamily;
        public string Typeface;
        public string PointSize;
        //[ ESC ( s 8.5 v ] Primary Font Pt Size : 8.5

        //[ESC(10 U ]        Primary Symbol Set : PC-8//字符集合
        //[ESC(s 0 p ]        Primary FontSpacing : Fixed Pitch//固定的像素
        //[ESC(s 17.25 h ]   Primary FontPitch : 17.25 chars per inch//字体大小
        //[ESC(s 0 s ] Primary FontStyle( 0 ) : Upright//字体格式
        //[ESC(s 0 b ] Primary FontWeight : Medium, Book or Text//粗细
        //[ESC(s 0 T ] Primary Font Typeface : Line Printer//字体的Typeface(模型）
    }
    public class StdPapers
    {
        public StdPapers(int width, int height)
        {
            Width300DPI = width;
            Height300DPI = height;
        }
        public int Width300DPI = 0;
        public int Height300DPI = 0;
    }
    public class PaperInfo
    {
        public PaperInfo()
        {

        }
        public PaperInfo(int width, int height)
        {
            Width300DPI = width;
            Height300DPI = height;
        }
        public string DoubleSide;
        string _PaperSize;
        public Dictionary<string, StdPapers> Papers;
        public string PaperSize
        {
            get
            {
                return _PaperSize;
            }
            set
            {
                _PaperSize = value;
                var qpg = from KeyValuePair<string, StdPapers> kp in Papers
                          where kp.Key == value
                          select kp;
                foreach (KeyValuePair<string, StdPapers> kp in qpg)
                {
                    Width300DPI = kp.Value.Width300DPI;
                    Height300DPI = kp.Value.Height300DPI;
                }
            }
        }
        public double PaperLengthInch = 1;
        public double TopMarginInch = 0;
        string _Orientation;
        public string Orientation
        {
            get
            {
                return _Orientation;

            }
            set
            {
                _Orientation = value;
                switch (_Orientation)
                {
                    case "Portrait":
                        break;
                    case "Landscape":
                        Rotate();
                        break;
                    case "Reverse Portrait":
                        break;
                    case "Reverse Landscape":
                        break;

                }

            }
        }
        public double Top = 0;
        public double Left = 0;
        public double HPDpi = 720;
        public double HPGDpi = 300;
        public double PCLUnit = 300;
        public double FONTCpi = 96;
        int _txtLg = 20;
        public int txtLg
        {
            get
            {
                return _txtLg;
            }
            set
            {
                _txtLg = value;
                if (value > 0)
                {
                    VMI = Convert.ToInt32((((PaperLengthInch - TopMarginInch) - 0.5) * 48) / _txtLg);
                }
                // _txtLg=(paperlg inch, top margin inch)-1/2 inch)*48/vmi

            }
        }
        public string Skip;
        public string Disable;
        public double LineSpace = 0;
        private double _VMI = 8;
        public double VMI
        {
            get
            {
                return _VMI;
            }
            set
            {
                if (value == 0)
                    return;
                _VMI = value;
            }
        }
        private double _HMI = 0.75*12; //Num b er of 120 1 inc h incremen ts.
        public double HMI
        {
            get
            {
                return _HMI;
            }
            set
            {
                if (value == 0)
                    return;
                _HMI = value;
            }
        }
        

        public double Width300DPI = 2480;
        double _Height300DPI = 3507;
        public double Height300DPI
        {
            get
            {
                return _Height300DPI;

            }
            set
            {
                _Height300DPI = value;
                PaperLengthInch = Height300DPI / PCLUnit;
            }
        }
        public void Rotate()
        {
            double v = Width300DPI;
            Width300DPI = Height300DPI;
            Height300DPI = v;
        }
        //char[] atl_Command = new char[] { 's', 'a', 'o', 'f', 'L', 'C' };
        //Reset
        //'s' 0单/双, 'a' 纸张大小 26a A4, 'o'//纸张方向1，横向, 'f'//文本长度100, 'L'Set Perforation Skip : Disable不用, 'C' 
    }
    public class LineInfo
    {
        public LineInfo()
        {

        }
        public int nLine = 0;
        public double LineStart = -1;
        public double x = 0;
        public double y = 0;
    }
    public class Position
    {
        public Position()
        {

        }
        public double x = 0.0;
        public double y = 0.0;
    }
    public class RectengleInfo
    {
        public RectengleInfo()
        {

        }

        public double XUnit = 0;
        public double YUnit = 0;
        public double HValue = 0;
        public double VValue = 0;
        public double Width = 0;
        public double Color = 0;
    }
    public partial class TPCLTOOLS
    {
        public double HPDpi
        {
            get
            {
                return cPaper.HPDpi;
            }
        }
        public int WPFDpi = 96;
        public int PNGDpi = 120;
        //& f 0 S Push/Pop Cursor Position (Push (Store))
        char[] af_Command = new char[] { 'S' };
        char[] atu_Command = new char[] { 'd', 'D' };
        char[] atk_Command = new char[] { 'g', 'G' };
        //Unit of Measure Command
        char[] atl_Command = new char[] { 's', 'S', 'a', 'o', 'f', 'G', 'L', 'C', 'X', 'U', 'Z', 'T' };

        /*
        //Reset
        //'s' 0单/双, 'a' 纸张大小 26a A4, 'o'//纸张方向1，横向, 'f'//文本长度100, 'L'Set Perforation Skip : Disable不用, 'C'
        //X number of copies
        //s/S: 单双面0 - Simplex
        //1 - Duplex, Long-Edge Binding
        //2 - Duplex, Short-Edge Binding
        // & l # UJob Separation # = The number of decipoints (1/720 inch)
        T Job Separation
        G
        */

        char[] ata_Command = new char[] { 'c', 'C', 'g', 'G', 'h', 'H', 'l', 'L', 'm', 'M', 'R', 'P', 'M', 'v', 'V' };
        /*
        //c:Absolute Move of X position by Column : 0 绝对左移动
        // R:Absolute Move of Y position by row : 0绝对右移动
        //<ESC>&a#C    X position by Column   
        ?&a#G
            # = 0 - Select next side
            1 - Select front side
            2 - Select back side
            ? & a # P
            # = 0 - 0° rotation.
            90 - 90° ccw rotation.
            180 - 180° ccw rotation.
            270 - 270° ccw rotation.
            Default = 0
            Range = 0, 90, 180, 270 (Other values ignored)
                */
        char[] starp_Command = new char[] { 'x', 'X', 'y', 'Y' };
        char[] starc_Command = new char[] { 'a', 'b', 'P' };

        char[] start_Command = new char[] { 'r', 'R' };
        char[] starv_Command = new char[] { 'T' };
        //Relative Move of Y by PCL dot unit : -50 相对Y移动 50
        char[] comma_s_Command = new char[] { 'p', 'h', 's', 'b', 'v', 'T' };

        char[] comma_Command = new char[] { 'U' };
        //[ESC(10 U ]        Primary Symbol Set : PC-8//字符集合
        //[ESC(s 0 p ]        Primary Font Spacing : Fixed Pitch//固定的像素
        //[ESC(s 17.25 h ]   Primary Font Pitch : 17.25 chars per inch//字体大小
        //[ESC(s 0 s ] Primary Font Style( 0 ) : Upright//字体格式
        //[ESC(s 0 b ] Primary Font Weight : Medium, Book or Text//粗细
        //[ESC(s 0 T ] Primary Font Typeface : Line Printer//字体的Typeface(模型）
        char[] c_Command = new char[] { 'a', 'b', 'P' };
        //[ ESC * c 3376 a ]  矩形水平的尺寸  Horizontal Rectangle Size, PCL units : 3376
        //[ESC * c 4 b]       矩形垂直尺寸   Vertical Rectangle Size, PCL units : 4
        //[ ESC * c P ]         Fill Rectangle : Black
        char[] atf_Command = new char[] { 'S' };
        char[] t_Command = new char[] { 'P' };
        char[] k_Command = new char[] { 'H' };//横向密度 1/120inch
        //[ ESC & f 0 S ] Push/Pop Cursor Position(Push (Store))
        public int cFontID = 0;
        public FontInfo cFont = new FontInfo();
        public PaperInfo cPaper;
        int EndPosition = 0;
        int ID = 0;
        int nLine = 0;
        LineInfo cLine = null;
        RectengleInfo cRect = null;
        Position cPos = new Position();
        Dictionary<string, StdPapers> Papers;
        public List<FontFamily> LocalFontFaimily;
    }

    public partial class TPCLTOOLS
    {
        public void PCLParse(string file4ex)
        {
            try
            {
                cPaper = new PaperInfo();
                cPaper.Papers = Papers;
                TBMain.Clear();
                cLine = new LineInfo();
                nLine = -3;
                cFontID = 0;
                int lastidx = -1;
                bool IsSingle = true;
                System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
                System.Text.Encoding encGB2312 = System.Text.Encoding.GetEncoding("GB2312");
                //string[] lines = System.IO.File.ReadAllLines(file4ex, encGB2312);
                string r = File.ReadAllText(file4ex, encGB2312);
                r = r.Replace("\r", "");
                string[] lines = r.Split('\n');
                foreach (string line in lines)
                {
                    Tracer.Info(nLine.ToString(), line);
                    nLine++;
                    cLine.nLine = nLine;
                    cLine.x = 0;
                    cLine.y = System.Convert.ToInt32(nLine * (cPaper.VMI + Convert.ToDouble(cPaper.LineSpace)) / 48 * cPaper.PCLUnit);
                    string[] splitted = line.Split('\u001b');
                    string command = "";
                    if (splitted.Length == 0 ||
                        line.Trim().Length == 0)//no pcl command
                    {
                        continue;
                    }
                    Tracer.Info(nLine.ToString(), line);
                    foreach (var split in splitted)
                    {
                        if (split.Length == 0) continue;
                        EndPosition = 0;
                        switch (split[0])
                        {
                            case '9'://Clear horizontal margin 所有边距晴空
                                IsSingle = true;
                                GetSingleCommand("9");
                                break;
                            case 'E':
                                IsSingle = true;
                                GetSingleCommand("E");
                                break;
                            case '&':
                                IsSingle = false;
                                switch (split[1])
                                {
                                    case 'l':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&l", split.Substring(2), atl_Command);
                                        break;
                                    case 'a':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&a", split.Substring(2), ata_Command);
                                        break;

                                    case 'f':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&f", split.Substring(2), atf_Command);
                                        break;
                                    case 't':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&t", split.Substring(2), t_Command);
                                        break;
                                    case 'k':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&k", split.Substring(2), k_Command);
                                        break;
                                    case 'u':
                                        lastidx = 2;
                                        EndPosition = lastidx;
                                        GetCommand("&l", split.Substring(2), atu_Command);
                                        break;
                                }
                                break;
                            case '*':
                                switch (split[1])
                                {
                                    case 'p':
                                        IsSingle = false;
                                        lastidx = 2; EndPosition = lastidx;
                                        GetCommand("*p", split.Substring(2), starp_Command);
                                        break;
                                    case 'c':
                                        IsSingle = false;
                                        lastidx = 2; EndPosition = lastidx;
                                        GetCommand("*c", split.Substring(2), starc_Command);
                                        break;
                                    case 't':
                                        IsSingle = false;
                                        lastidx = 2; EndPosition = lastidx;
                                        GetCommand("*t", split.Substring(2), start_Command);
                                        break;
                                    case 'v':
                                        IsSingle = false;
                                        lastidx = 2; EndPosition = lastidx;
                                        GetCommand("*v", split.Substring(2), starv_Command);
                                        break;
                                }
                                break;
                            case '('://假设都是从字符集开始，接下来是s
                                switch (split[1])
                                {
                                    case 's':
                                        IsSingle = false;
                                        lastidx = 2; EndPosition = lastidx;
                                        GetCommand("(s", split.Substring(2), comma_s_Command);
                                        break;
                                    default:
                                        IsSingle = true;
                                        lastidx = 1; EndPosition = lastidx;
                                        GetFontSetCommand("(FontSet", split.Substring(1));
                                        break;
                                }
                                break;
                            default:
                                break;
                        }
                        if (IsSingle) continue;
                        string value = split.Substring(EndPosition);
                        if (value == "") continue;
                        if (value.Contains("27"))
                        {

                        }
                        dwData = TBMain.NewRow();
                        TMV.SetUpNewRow(ref dwData);
                        dwData["PRCTYPE"] = "TXT";
                        dwData["PRCOND"] = nLine;
                        dwData["PRCONTENT"] = Helper.ConvertGB2312To8859P1(value);
                        dwData["PRFONT"] = cFont.CFontID;

                        dwData["PRLX"] =Helper.RoundX( cLine.x,1);
                        cLine.x = cLine.x + System.Convert.ToInt32(cPaper.FONTCpi * 2.54 / cFont.FontPitch * value.Length);//下个字符的开始位置，按照PCL Unit计算
                        dwData["PRFONTSIZE"] = Helper.RoundX(cLine.x, 1);
                        dwData["PRLY"] = Helper.RoundX(cLine.y, 1);
                        //dwData["PRLY"] = nLine * cPaper.VMI / 48 * cPaper.PCLUnit;  
                        dwData["ROWNID"] = ID++;
                        TBMain.Rows.Add(dwData);
                    }
                }
                foreach (DataRow row in TBMain.Rows)
                {
                    /*
                    row["PRLY"] = System.Convert.ToInt32(double.Parse(row["PRCOND"].ToString())
                        * (cPaper.VMI + Convert.ToDouble(cPaper.LineSpace)) / 48 * cPaper.PCLUnit);*/

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        string result = "";
        public void SetFont(string command, string value)
        {
            switch (command)
            {
                case "(sp":
                    cFont.FontSpacing = value;
                    break;
                case "(sh":
                    cFont.FontPitch = double.Parse(value);
                    break;
                case "(ss":
                    cFont.FontStyle = value;
                    break;
                case "(sb":
                    cFont.FontWeight = value;
                    break;
                case "(sv":
                    cFont.PointSize = value;
                    break;
                case "(sT":
                    string fontfamily = "";
                    switch (value)
                    {
                        case "4099":
                            fontfamily = "Courier";
                            break;
                        default:
                        case "4101":
                            fontfamily = "CG Times";
                            break;
                        case "4102":
                            fontfamily = "LetterGothic";
                            break;
                        case "16602":
                            fontfamily = "Arial";
                            break;
                        case "4362":
                            fontfamily = "AlbertusMedium";
                            break;
                        case "4297":
                            fontfamily = "Marigold";
                            break;
                        case "4197":
                            fontfamily = "GaramondAntiqua";
                            break;
                        case "4168":
                            fontfamily = "AntiqueOlive";
                            break;
                        case "4148":
                            fontfamily = "UniversMedium";
                            break;
                        case "4140":
                            fontfamily = "ClarendonCondense";
                            break;
                        case "4116":
                            fontfamily = "Coronet";
                            break;
                        case "4113":
                            fontfamily = "CGOmega";
                            break;
                        case "16901":
                            fontfamily = "TimesNewRoman";
                            break;
                        case "24579":
                            fontfamily = "Courier";
                            break;
                        case "16686":
                            fontfamily = "SymbolMT";
                            break;
                        case "45099":
                            fontfamily = "ZapfChancery-MediumItalic";
                            break;
                        case "25093":
                            fontfamily = "Times-Roman";
                            break;
                        case "24703":
                            fontfamily = "NewCenturySchlbk-Roman";
                            break;
                        case "24623":
                            fontfamily = "Bookman - Light";
                            break;
                        case "24607":
                            fontfamily = "AvantGarde-Book";
                            break;
                        case "24591":
                            fontfamily = "Palatino-Roman";
                            break;
                        case "24580":
                            fontfamily = "Helvetica";
                            break;
                        case "45358":
                            fontfamily = "SymbPS";
                            break;
                        case "31402":
                            fontfamily = "Wingdings-Regular";
                            break;
                        case "45101":
                            fontfamily = "ZapfDingbats";
                            break;
                        case "37110":
                            fontfamily = "FangSong GB2312";
                            break;
                    }
                    bool IsFound = false;
                    string first = "";
                    if (LocalFontFaimily== null)
                    {
                        LocalFontFaimily = new List<FontFamily>();
                        foreach (System.Windows.Media.FontFamily fm in System.Windows.Media.Fonts.GetFontFamilies(@"D:\urwfonts\"))
                        {
                            foreach (var name in fm.FamilyNames)
                            {
                                Tracer.Info("font:", name.Value);
                            }
                            LocalFontFaimily.Add(fm);
                        }
                    }
                    foreach (System.Windows.Media.FontFamily fm in LocalFontFaimily)
                    {
                        if (IsFound)
                            break;
                        foreach (var name in fm.FamilyNames)
                        {
                            if (name.Value == first)
                            {
                                cFont.FontFamily = "LetterGothic";
                                break;
                            }
                            if (first == "") first = name.Value;

                            if (name.Value == fontfamily)
                            {
                                cFont.FontFamily = fontfamily;
                                IsFound = true;
                                break;
                            }
                        }
                    }
                    if (!IsFound)
                    {
                        Tracer.Info("font is not found:", fontfamily);
                    }
                    cFont.Typeface = value;
                    break;
            }
            if (command == "(sT")
            {
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["PRCTYPE"] = "FONT";
                dwData["PRCONTENT"] = cFont.SymbolSet + ";" + cFont.Typeface + ";" + cFont.FontWeight + ";" + cFont.FontStyle + ";" + cFont.FontPitch + ";" + cFont.FontSpacing;
                dwData["ROWNID"] = ID++;
                dwData["PRCOND"] = 0;
                dwData["PRCTYPDES"] = cFont.FontFamily;
                dwData["PRLY"] = cFont.CFontID;
                TBMain.Rows.Add(dwData);
                return;
            }
        }
        public void SetPaper(string command, string value)
        {
            //char[] atl_Command = new char[] { 's', 'a', 'o', 'f', 'L', 'C' };
            //Reset
            //'s' 0单/双, 'a' 纸张大小 26a A4, 'o'//纸张方向1，横向, 'f'//文本长度100, 'L'Set Perforation Skip : Disable不用, 'C'
            switch (command)
            {
                case "&ls":
                case "&lS":
                    cPaper.DoubleSide = value;
                    break;
                case "&la":

                    switch (value)
                    {
                        default:
                        case "26":
                            cPaper.PaperSize = "A4";
                            break;
                        case "1":
                            cPaper.PaperSize = "EXECUTIVE";
                            break;
                        case "2":
                            cPaper.PaperSize = "LETTER";
                            break;
                        case "3":
                            cPaper.PaperSize = "LEGAL";
                            break;
                        case "6":
                            cPaper.PaperSize = "LEDGER";
                            break;
                        case "27":
                            cPaper.PaperSize = "A3";
                            break;
                        case "80":
                            cPaper.PaperSize = "Monarch";
                            break;
                        case "81":
                            cPaper.PaperSize = "COM-10";
                            break;
                        case "90":
                            cPaper.PaperSize = "DL";
                            break;
                        case "91":
                            cPaper.PaperSize = "C5";
                            break;
                        case "100":
                            cPaper.PaperSize = "B5";
                            break;
                    }
                    /*
                     # = 1 - Executive (7¼ x 10½ in.)
                     2 - Letter (8½ x 11 in.)
                     3 - Legal (8½ x 14 in.)
                     6 - Ledger (11 x 17 in.)
                     26 - A4 (210mm x 297mm)
                     27 - A3 (297mm x 420mm)
                     # = 80 - Monarch (Letter - 3 7/8 x 7½ in.)
                        81 - Com-10 (Business - 4 1/8 x 9½ in.)
                        90 - International DL (110mm x 220mm)
                        91 - International C5 (162mm x 229mm)
                        100 - International B5 (176mm x 250mm)
                        Range = 1, 2, 3, 6, 26, 27, 80, 81, 90, 91, 100 (Other values ignored)
                     * */
                    break;
                case "&lo":
                    switch (value)
                    {
                        default:
                        case "0":
                            cPaper.Orientation = "Portrait";
                            break;
                        case "1":
                            cPaper.Orientation = "Landscape";
                            break;
                        case "2":
                            cPaper.Orientation = "Reverse Portrait";
                            break;
                        case "3":
                            cPaper.Orientation = "Reverse Landscape";
                            break;
                    }
                    break;
                case "&lD":
                    cPaper.PCLUnit = int.Parse(value);
                    /* Default = 300
                     * Range = 96, 100, 120, 144, 150, 160, 180, 200, 225, 240, 288, 300, 360,400, 450, 480, 600, 720, 800, 900, 1200, 1440, 1800, 2400, 3600,7200.                       */
                    break;
                case "&lf":
                    cPaper.txtLg = int.Parse(value);
                    break;
                //# = 0 - Disable
                ///1 - Enable
                //Whenever the perforation skip mode is changed, the top margin and
                //page length are returned to their default values.
                /*
                 * The perforation region is the distance from the bottom of the text area
                    of one page to the top of the text area (top margin) of the next page.
                    When perforation skip is enabled, a Line Feed or Half-Line Feed,
                    which would move the cursor beyond the bottom of the text area,
                    causes the cursor to move to the top of the text area on the next page.
                    When perforation skip is disabled, a Line Feed or Half-Line Feed
                    allows the cursor to move to the next line or half-line in the perforation
                    region, allowing printing to continue there
                 */
                case "&lC"://# = number of 1/48 inch increments between rows
                    cPaper.LineSpace = int.Parse(value);
                    break;
                case "*tr":
                case "*tR":
                    cPaper.HPGDpi = int.Parse(value);
                    break;
                case "&lU"://Left Offset The number of decipoints (1/720 inch)
                case "&lX"://copies
                case "&lZ"://Top Offset  The number of decipoints (1/720 inch) 
                case "&lT"://Job Separation
                    break;
            }
            if (command == "&lL" || command == "&lC")
            {
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["PRCTYPE"] = "Paper";
                dwData["PRCOND"] = 0;
                dwData["PRCONTENT"] = cPaper.PaperSize + ";" + cPaper.Orientation + ";" + cPaper.DoubleSide + ";" + cPaper.txtLg + ";" + cPaper.Skip + ";" + cPaper.Disable;
                dwData["ROWNID"] = ID++;
                TBMain.Rows.Add(dwData);
                return;
            }
        }
        public void GetCommand(string cmd, string src, char[] commands)
        {

            int idx = src.IndexOfAny(commands);
            if (idx < 0)
            {
                return;
            }
            string command = "";
            string value = "";
            string result = "";
            command = cmd + src.Substring(idx, 1);
            value = src.Substring(0, idx);
            EndPosition += idx + 1;
            switch (cmd)
            {
                case "(s":
                    SetFont(command, value);
                    break;
                case "*t":
                case "&l":
                    SetPaper(command, value);
                    SetPosition(command, value);
                    break;
                case "&f":
                case "&k":
                case "*p":
                case "&a":
                    SetPosition(command, value);
                    break;
                case "*c":
                    SetRect(command, value);
                    break;
                default:
                    dwData = TBMain.NewRow();
                    TMV.SetUpNewRow(ref dwData);
                    dwData["PRCTYPE"] = command;
                    dwData["PRCONTENT"] = value;
                    dwData["ROWNID"] = ID++;
                    dwData["PRCOND"] = nLine;
                    TBMain.Rows.Add(dwData);
                    break;
            }
            if (command.ToUpper() == "(ST"
                || command.ToUpper() == "&AC"
                || command.ToUpper() == "&TP"
                || command.ToUpper() == "&KH"
                || command.ToUpper() == "*VT"
                || command.ToUpper() == "&AR")
            {
                return;
            }
            result = src.Substring(idx + 1);
            idx = src.IndexOfAny(commands);
            GetCommand(cmd, result, commands);
            return;
        }
        public void GetFontSetCommand(string cmd, string src)
        {
            cFont = new FontInfo();
            cFont.CFontID = cFontID++;
            string command = "";
            string value = "";
            string result = "";
            command = cmd;
            value = src.Substring(0);
            cFont.SymbolSet = value;
        }
        public System.Windows.Media.Typeface GetFontFaimily(string fontfamily)
        {
            bool IsFound = false;
            System.Windows.Media.Typeface FM = null;
            foreach (System.Windows.Media.Typeface tp in System.Windows.Media.Fonts.GetTypefaces(@"D:\urwfonts\"))
            {
                if (IsFound)
                    break;
                foreach (var name in tp.FontFamily.FamilyNames)
                {
                    //Tracer.Info("Fonts", name.Value);
                    if (name.Value == fontfamily)
                    {
                        IsFound = true;
                        FM = tp;
                        break;
                    }
                }
            }
            return FM;
        }
        public void SetPosition(string command, string value)
        {
            switch (command)
            {

                case "&lL":
                    cPos.x = 0;
                    cPos.y = 0;
                    break;
                //# = Number of PCL Units ,1/300 //Unit of Measure
                case "*px":
                    if (value == "") value = "0";
                    cPos.x = int.Parse(value);
                    cLine.x = cPos.x;
                    break;
                case "*py":
                case "*pY":
                    cPos.y = int.Parse(value);
                    cLine.y = cPos.y;
                    break;
                case "&fS":
                    /*
                     The Push/Pop Cursor Position command allows the current cursor position to be stored and recalled.
                     ? & f # S
                     # = 0 - Push (Store cursor position)
                     1 - Pop (Recall a cursor position)
                     */

                    break;
                case "&kg":
                case "&kG":
                    /*# = 0- CR=CR; LF=LF; FF=FF
                    //                    1 - CR = CR - LF; LF = LF; FF = FF
                    2 - CR = CR; LF = CR - LF; FF = CR - FF
                        3 - CR = CR - LF; LF = CR - LF; FF = CR - FF
                        CR 回车在相同列的地方，LF一行结束，从最左边开始，FF一页结束
                            */
                    cPos.y = int.Parse(value);
                    cLine.y = cPos.y;
                    break;
                case "&ac":
                case "&aC"://This Horizontal Cursor Positioning command moves the cursor to a new column on the current line
                    if (value == "")
                    {
                    }
                    else
                    {
                        cPos.x = (double.Parse(value)*cPaper.HMI)/120*cPaper.PCLUnit;
                        cLine.x = cPos.x;
                    }
                    break;
                case "&ar":
                case "&aR":
                    if (value == "")
                    {
                    }
                    else
                    {

                    }
                    //This Vertical Cursor Positioning command moves the cursor to a new                     line in the same column position.? &a # R # = Number of Rows
                    //相当于回车几次 行数，行高在VMI定义
                    break;
                case "&aG":

                    break;
                case "&aH"://This Horizontal Cursor Positioning command moves the cursor to a new position along the horizontal axis.
                    //1/720 DPI
                    break;
                case "&aP":

                    break;
                case "&aL"://左边距离 行数

                    break;
                case "&aM"://右边距

                    break;
                case "&aV":
                    /*
                     * This Vertical Cursor Positioning command moves the cursor to a new position along the vertical axis.
? & a # V
# = Number of Decipoints (1/720 inch)
                     */

                    break;
                    //char[] ata_Command = new char[] { 'c', 'R', 'C', 'G', 'P' };
                    /*
                    //c:Absolute Move of X position by Column : 0 绝对左移动
                    // R:Absolute Move of Y position by row : 0绝对右移动
                    //<ESC>&a#C    X position by Column   
                    ?&a#G
                        # = 0 - Select next side
                        1 - Select front side
                        2 - Select back side
                        ? & a # P
                        # = 0 - 0° rotation.
                        90 - 90° ccw rotation.
                        180 - 180° ccw rotation.
                        270 - 270° ccw rotation.
                        Default = 0
                        Range = 0, 90, 180, 270 (Other values ignored)
                            */

            }
        }

        public void SetRect(string command, string value)
        {
            switch (command)
            {
                case "*ca":
                case "*cA":
                    cRect = new RectengleInfo();
                    cRect.XUnit = cPos.x;
                    cRect.YUnit = cPos.y;
                    cRect.HValue = int.Parse(value);
                    break;
                case "*cb":
                    cRect.VValue = int.Parse(value);
                    break;
                case "*cP":
                    cRect.Color = 0;
                    break;
            }
            if (command == "*cP")
            {
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["PRCTYPE"] = "RECT";
                dwData["PRCONTENT"] = cRect.XUnit + ";" + cRect.YUnit + ";" + cRect.HValue + ";" + cRect.VValue + ";" + cRect.Color + ";";
                dwData["ROWNID"] = ID++;
                dwData["PRCOND"] = 0;
                dwData["PRLY"] = 0;
                TBMain.Rows.Add(dwData);
                return;
            }
        }
        public void GetSingleCommand(string cmd)
        {
            string command = "";
            string value = "";
            string result = "";
            command = cmd;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["PRCTYPE"] = command;
            dwData["PRCONTENT"] = "";
            dwData["ROWNID"] = ID++;
            dwData["PRCOND"] = nLine;
            TBMain.Rows.Add(dwData);
            return;
        }

    }

}
